package net.blank.ui
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	import net.blank.bitmapOperation.BitmapDraw;
	import net.blank.functionUtl.FunctionBinder;
	import net.blank.ui.core.Skin;
	
	/**
	 * 按钮皮肤
	 * @author blank
	 * 
	 */
	public class ButtonSkin extends Skin
	{
		/**
		 * 鼠标位于上方时的背景图像
		 */
		private var _overBackGround:BitmapData;
		
		/**
		 * 鼠标位于上方时的滤镜
		 */
		private var _overFilters:Array;
		
		/**
		 * 鼠标按下时的背景图像
		 */
		private var _downBackGround:BitmapData;
		
		/**
		 * 鼠标按下时的滤镜
		 */
		private var _downFilters:Array;
		
		/**
		 * 不可接受用户交互事件时的背景图像
		 */
		private var _disabledBackGround:BitmapData;
		
		/**
		 * 鼠标位于上方时9格切片数据
		 */
		protected var _overClipCache:Object;
		
		/**
		 * 鼠标按下时9格切片数据
		 */
		protected var _downClipCache:Object;
		
		/**
		 * 不可接受用户交互事件时9格切片数据
		 */
		protected var _disabledClipCache:Object;
		
		/**
		 * 按钮皮肤
		 * @param background 背景图像
		 * @param innerScaleRect 九格缩放时的内部区域
		 * @param overBackGround 鼠标位于上方时的背景图像
		 * @param downBackGround 鼠标按下时的背景图像
		 * @param disabledBackGround 不可接受用户交互事件时的背景图像
		 * @param overFilters 鼠标位于上方时的滤镜
		 * @param downFilters 鼠标按下时的滤镜
		 * @param disabledFilters 禁用交互性事件时的滤镜
		 * 
		 */
		public function ButtonSkin(
			background:BitmapData=null, 
			innerScaleRect:Rectangle=null, 
			overBackGround:BitmapData=null,
			downBackGround:BitmapData=null,
			disabledBackGround:BitmapData=null,
			overFilters:Array=null,
			downFilters:Array=null,
			disabledFilters:Array=null
		){
			super(background, innerScaleRect, disabledFilters);
			_overBackGround=overBackGround;
			_downBackGround=downBackGround;
			_disabledBackGround=disabledBackGround;
			_overFilters=overFilters;
			_downFilters=downFilters;
		}
		
		/**
		 * 鼠标位于上方时的背景图像
		 */
		public function setOverBackGround(value:BitmapData):void{
			if(value != _overBackGround){
				_overBackGround=value;
				
				if(value){
					if(_innerScaleRect){
						_overClipCache=BitmapDraw.sliceBitmapForScale9Gride(value,_innerScaleRect);
					}
				}else{
					_overClipCache=null;
				}
				
				FunctionBinder.execCallbackFun(setOverBackGround,[value]);
			}
		}
		public function getOverBackGround():BitmapData{
			return _overBackGround;
		}
		
		/**
		 * 鼠标位于上方时的滤镜
		 */
		public function setOverFilters(value:Array):void{
			if(value != _overFilters){
				_overFilters=value;
				
				FunctionBinder.execCallbackFun(setOverFilters,[value]);
			}
		}
		public function getOverFilters():Array{
			return _overFilters;
		}
		
		/**
		 * 鼠标按下时的背景图像
		 */
		public function setDownBackGround(value:BitmapData):void{
			if(value != _downBackGround){
				_downBackGround=value;
				
				if(value){
					if(_innerScaleRect){
						_downClipCache=BitmapDraw.sliceBitmapForScale9Gride(value,_innerScaleRect);
					}
				}else{
					_downClipCache=null;
				}
				
				FunctionBinder.execCallbackFun(setDownBackGround,[value]);
			}
		}
		public function getDownBackGround():BitmapData{
			return _downBackGround;
		}
		
		/**
		 * 鼠标按下时的滤镜
		 */
		public function setDownFilters(value:Array):void{
			if(value != _downFilters){
				_downFilters=value;
				
				FunctionBinder.execCallbackFun(setDownFilters,[value]);
			}
		}
		public function getDownFilters():Array{
			return _downFilters;
		}
		
		/**
		 * 不可接受用户交互事件时的背景图像
		 */
		public function setDisabledBackGround(value:BitmapData):void{
			if(value != _disabledBackGround){
				_disabledBackGround=value;
				
				if(value){
					if(_innerScaleRect){
						_disabledClipCache=BitmapDraw.sliceBitmapForScale9Gride(value,_innerScaleRect);
					}
				}else{
					_disabledClipCache=null;
				}
				
				FunctionBinder.execCallbackFun(setDisabledBackGround,[value]);
			}
		}
		public function getDisabledBackGround():BitmapData{
			return _disabledBackGround;
		}
		
		override public function setInnerScaleRect(value:Rectangle):void{
			if(value != _innerScaleRect){
				_innerScaleRect=value;
				
				if(value){
					if(_background){
						_clipCache=BitmapDraw.sliceBitmapForScale9Gride(_background,value);
					}
					if(_overBackGround){
						_overClipCache=BitmapDraw.sliceBitmapForScale9Gride(_overBackGround,value);
					}
					if(_downBackGround){
						_downClipCache=BitmapDraw.sliceBitmapForScale9Gride(_downBackGround,value);
					}
					if(_disabledBackGround){
						_disabledClipCache=BitmapDraw.sliceBitmapForScale9Gride(_disabledBackGround,value);
					}
				}else{
					_clipCache=null;
					_overClipCache=null;
					_downClipCache=null;
					_disabledClipCache=null;
				}
				
				FunctionBinder.execCallbackFun(setInnerScaleRect,[value]);
			}
		}
		
		/**
		 * 鼠标位于上方时9格切片数据
		 */
		public function get overClipCache():Object{
			return _overClipCache;
		}
		
		/**
		 * 鼠标按下时9格切片数据
		 */
		public function get downClipCache():Object{
			return _downClipCache;
		}
		
		/**
		 * 不可接受用户交互事件时9格切片数据
		 */
		public function get disabledClipCache():Object{
			return _disabledClipCache;
		}
		
		override public function destroy():void{
			if(_overBackGround){
				_overBackGround.dispose();
				_overBackGround=null;
			}
			_overClipCache=null;
			_overFilters=null;
			
			if(_downBackGround){
				_downBackGround.dispose();
				_downBackGround=null;
			}
			_downClipCache=null;
			_downFilters=null;
			
			if(_disabledBackGround){
				_disabledBackGround.dispose();
				_disabledBackGround=null;
			}
			_disabledClipCache=null;
			
			FunctionBinder.clearBindingFuns(
				setOverBackGround,
				setOverFilters,
				setDownBackGround,
				setDownFilters,
				setDisabledBackGround
			);
			
			super.destroy();
		}
		
		override public function clone():*{
			return new ButtonSkin(
				_background.clone(),
				_innerScaleRect.clone(),
				_overBackGround.clone(),
				_downBackGround.clone(),
				_disabledBackGround.clone(),
				cloneFilters(_overFilters),
				cloneFilters(_downFilters),
				cloneFilters(_disabledFilters)
			);
		}
	}
}